<template>
  <basic-dialog
    width="60%"
    title="选择品牌"
    :visible="visible"
    :showFooter="false"
    :handleClose="handleCloseDialog"
  >
    <BasicForm
      :colNumOfRow="2"
      :isScalingBtn="false"
      :hasQueryPermission="false"
      :formColumns="formItemList"
      :dataSource="formData"
      :handleSearchForm="handleQueryForm"
      @getFormData="getFormData"
    />
    <BasicTable
      showActionCol
      :showSelectCol="false"
      :loading="tableInfo.loading"
      :columns="tableInfo.columns"
      :dataSource="tableInfo.list"
      :pagination="pagination"
      :pageSizes="pageSizes"
      :tableHeight="480"
      :actionRender="renderTableBtnAction"
      @pageChange="handlePageChange"
      @pageSizeChange="handlePageSizeChange"
    />
  </basic-dialog>
</template>
<script>
import BasicDialog from '@/components/BasicDialog';
import BasicForm from '@/components/BasicForm';
import BasicTable from '@/components/BasicTable';
import basePage from '@/mixins/commodity/basePage';
import { getTableAction } from '@/utils/getBtnsRender';
import { getEchoTextByValue } from '@/utils/getUtils';
import { getBrandInfoListService } from '@/api/commodity/common';
export default {
  mixins: [basePage],
  components: {
    BasicDialog,
    BasicForm,
    BasicTable
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    canSearchAll: { // 查询所有品牌，默认不查所有
      type: Boolean,
      default: false
    },
    categoryUuid: { // 品类uuid
      type: String
    }
  },
  data () {
    return {
      formData: { brandName: '' }, // 品牌名称
      formItemList: [{ label: '品牌名称', prop: 'brandName', type: 'input' }],
      tableInfo: {
        list: [],
        columns: [
          { title: '品牌名称',  key: 'brandName' },
          { title: '厂家品牌名称',  key: 'factoryBrandName' },
          {
            title: '厂家品牌影响力',
            render: (h, { row }) => {
              return <span>{this.getFactoryInfluenceField(row)}</span>
            }
          }
        ],
        loading: false
      }
    }
  },
  watch: {
    visible (newVal) {
      newVal && this.getTableData();
    }
  },
  methods: {
    // 渲染表格按钮
    renderTableBtnAction (h, { row }) {
      const actionList = [{
        label: '选择',
        method: this.handleChooseBrand
      }];
      return getTableAction({ h, row, actionList })
    },
    // 获取厂家品牌影响力字段
    getFactoryInfluenceField (row) {
      return getEchoTextByValue({
        val: row.factoryInfluenceMore,
        firstText: '大于品牌',
        secondText: '小于品牌'
      });
    },
    // 获取表单查询入参
    getSearchFormParams () {
      const { nowPage, pageShow } = this.pagination;
      const categoryUuid = this.canSearchAll ? '' : (this.categoryUuid || this.$attrs.categoryUuid);
      return {
        nowPage,
        pageShow,
        categoryUuid,
        notInUuids: '',
        brandName: this.formData.brandName
      }
    },
    // 获取表格数据
    async getTableData () {
      const data = this.getSearchFormParams();
      const loadingFn = state => { this.tableInfo.loading = state };
      const res = await getBrandInfoListService(data, loadingFn);
      if (res?.retData) {
        this.tableInfo.list = res?.retData?.results ?? [];
        this.pagination.totalNum = res?.retData?.totalNum ?? 0;
      }
    },
    // 获取已选品牌信息
    getSelectedBrandInfo (row, isMoreFactoryInfluence) {
      const { brandName, uuid, factoryBrandName, productNameType, hasBarCode } = row;
      return {
        uuid,
        name: brandName,
        joinBrandName: isMoreFactoryInfluence ? factoryBrandName : brandName,
        productNameType: productNameType,
        hasBarCode: hasBarCode,
        factoryInfluenceMore: row?.factoryInfluenceMore,
        factoryBrandName: row?.factoryBrandName
      };
    },
    // 选择品牌
    handleChooseBrand (row) {
      const isMoreFactoryInfluence = row.factoryInfluenceMore === '1'; // 厂家品牌影响力大于商品品牌
      const isNullFactoryBrandName = !row.factoryBrandName; // 厂家品牌名称为空
      if (isMoreFactoryInfluence && isNullFactoryBrandName) {
        return this.$message.warning('厂家品牌影响力大于商品品牌时，厂家品牌名称不能为空');
      }
      const brandInfo = this.getSelectedBrandInfo(row, isMoreFactoryInfluence);
      this.$emit('getBrandInfo', brandInfo);
      this.handleCloseDialog();
    },
    // 关闭弹框
    handleCloseDialog () {
      this.$emit('update:visible', false);
    },
  }
}
</script>
<style lang="less" scoped>
/deep/ .el-dialog__header {
  border-bottom: 1px solid #e8eaec;
}
/* 分页样式 */
.pagination_wrap {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}
</style>
